package mamikeji.liaowa.utils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.util.UUID;

/**
 * Created by 廉冲-android开发 on 2017/5/11.
 * 获取设备ID
 */
public class ForAndroidID {
    public static String getDeviceId(Context context) {

        String android_ID = ForPreference.getString(ForContents.GET_ANDROID_ID);

        if (android_ID != null && !"".equals(android_ID)) {
            return android_ID;
        }
        // Android Id
        if (android_ID == null || "".equals(android_ID)) {
            try {
                android_ID = getAndroidId(context).replace(":", "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // Build
        if (android_ID == null || "".equals(android_ID)) {
            try {
                android_ID = getBuildSerial();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //获取 IMEI码
        if (android_ID == null || "".equals(android_ID)) {
            try {
                android_ID = getIMIEStatus(context).replace(":", "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // Mac地址
        if (android_ID == null || "".equals(android_ID)) {
            try {
                android_ID = getLocalMac(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // UUID生成唯一码
        if (android_ID == null || "".equals(android_ID)) {
            UUID uuid = UUID.randomUUID();
            android_ID = uuid.toString().replace("-", "");
        }
        return android_ID;
    }

    // IMEI码
    private static String getIMIEStatus(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String android_ID = tm.getDeviceId();
        return android_ID;
    }

    // Mac地址
    private static String getLocalMac(Context context) {
        WifiManager wifi = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }

    // Android Id
    private static String getAndroidId(Context context) {
        String androidId = Settings.Secure.getString(
                context.getContentResolver(), Settings.Secure.ANDROID_ID);
        return androidId;
    }

    //Build.SERIAL
    private static String getBuildSerial() {
        String androidId = Build.SERIAL;

        return androidId;
    }
}
